<template>
  <!-- 添加或修改菜单对话框 -->
  <el-dialog :title="!form.id ? '上传' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <div style="text-align: center;">
      <el-upload class="upload-demo" drag action="/admin/sys-file/upLodeFileAdd" multiple>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
      </el-upload>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">取 消</el-button>
      <el-button size="small" type="primary" @click="dataFormSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDict } from '~/utils'
import { addObj, getDivision, getObj, putObj, getObjByCode } from '@/admin/api/division'

export default {
  name: "serverDeployAdd",
  data () {
    return {
      // 遮罩层
      loading: true,
      // 是否显示弹出层
      visible: false,
      form: {
        name: '',
        id: '',
        ip: '',
        port: '',
        divisionCode: '',
        hasChildren: true
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        ip: [
          { required: true, message: "IP不能为空", trigger: "blur" }
        ]
      },
      options: [],
    }
  },
  created () {

  },
  methods: {
    loadOptions ({ action, parentNode, callback, instanceId }) {
      this.data = [];
      getDivision({ lazy: true, parentId: parentNode.divisionCode }).then(response => {
        this.data = response.data.data
        this.data.map(el => {
          for (let key in el) {
            el.id = el.divisionCode,
              el.label = el.divisionName
          }
        })
        parentNode.children = this.data
        callback()
      })

    },

    init (isEdit, data) {

      this.options = getDict('DIVISION_LEVEL');

      this.visible = true
      this.$nextTick(() => {
        if (isEdit) {
          getObj(data.id).then(response => {
            let res = response.data
            this.form = {
              parentId: res.data.parentCode,
              id: res.data.divisionId,
              divisionName: res.data.divisionName,
              divisionLevel: res.data.divisionLevel,
              divisionCode: res.data.divisionCode,
              hasChildren: true,
            }

          })
        } else {
          this.form = {
            parentId: this.form.parentId,
            id: '',
            divisionName: '',
            divisionLevel: '',
            divisionCode: '',
            hasChildren: true
          }
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.form.parentId === undefined) {
            this.form.parentCode = '000000'
          } else {
            this.form.parentCode = this.form.parentId
          }
          if (this.form.id) {
            this.form.divisionId = this.form.id
            putObj(this.form).then(data => {
              this.$message.success('修改成功')
              this.visible = false
              this.$emit('refreshDataList')
            })
          } else {
            addObj(this.form).then(data => {
              this.$message.success('添加成功')
              this.visible = false
              this.$emit('refreshDataList')
            })
          }
        }
      })
    },

  }
}
</script>
